import Vue from "vue";
import Vuex from "vuex";
import createLogger from "vuex/dist/logger";
import http from '@/api';

Vue.use(Vuex)
let env = process.env.NODE_ENV,
    plugins = [];
if (env === 'development') plugins.push(createLogger());

const store = new Vuex.Store({
    state: {
        user: null,
        // 控制左侧导航收缩
        collapse: false,
        // 标签页数据
        editableTabsValue: "0",
        editableTabs: [
            {
                title: "首页",
                name: "0",
            }
        ],
        tabIndex: 0,
        titleName: "",
    },
    mutations: {
        // 控制左侧导航收缩
        change(state) {
            state.collapse = !state.collapse;
        },
        setUser(state, payload) {
            state.user = payload;
            // console.log(state.user, 'store');
        },

        // 标签页相关
        addTab(state, targetName) {
            if (location.hash.split("/")[2] !== targetName.path) {
                let newTabName = ++state.tabIndex + "";
                state.editableTabs.push({
                    title: targetName.name,
                    name: newTabName,
                    path: targetName.path
                });
                state.editableTabsValue = newTabName;
            }

        },
    },
    actions: {
        async setUserAsync({ commit }) {
            let result;
            try {
                result = await http.getInfo();
                if (+result.code !== 200) return null;
                /* if (result) {
                    console.log(result.user);
                    result.user
                } */
                commit('setUser', result?.user?.userName);
            } catch (_) { console.log(_) }
            // console.log(result?.user?.userName);
            return result?.user?.userName
        }
    },


});
export default store;